package asn.match.dao;


import java.util.List;

import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceContextType;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import asn.match.model.MemberAccount;

@Repository("memberAccountDAO")
@PersistenceContext(type = PersistenceContextType.EXTENDED)
@Transactional
public class MemberAccountDaoImpl implements MemberAccountDAO {

	@Autowired
	private SessionFactory sessionFactory;

	@Override
	public void addMemberAccount(MemberAccount memberAccount) {
		this.sessionFactory.getCurrentSession().save(memberAccount);
	}

	@Override
	public void updateMemberAccount(MemberAccount memberAccount) {
		this.sessionFactory.getCurrentSession().update(memberAccount);
	}

	@Override
	public void deleteMemberAccount(MemberAccount memberAccount) {
		//Penser à mettre l'annotation Cascade
		this.sessionFactory.getCurrentSession().delete(memberAccount);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<MemberAccount> findAllMemberAccounts() {
		return (List<MemberAccount>) this.sessionFactory.getCurrentSession().createQuery("from MemberAccount member_account").list();
	}

	@Override
	public MemberAccount findMemberAccountById(Integer memberAccountId) {
		return (MemberAccount) this.sessionFactory.getCurrentSession().get(MemberAccount.class, memberAccountId);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<MemberAccount> findMemberAccountByLinkname(String linkname) {
		return (List<MemberAccount>) this.sessionFactory.getCurrentSession().createQuery("from MemberAccount member_account where linkname=?").setString(0, linkname).list();	
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<MemberAccount> findMemberAccountByValidationCode(String validationCode) {
		return (List<MemberAccount>) this.sessionFactory.getCurrentSession().createQuery("from MemberAccount member_account where validation_code=?").setString(0, validationCode).list();	
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<MemberAccount> findMemberAccountByEmail(String email) {
		return (List<MemberAccount>) this.sessionFactory.getCurrentSession().createQuery("from MemberAccount member_account where email=?").setString(0, email).list();	
	}
}
